package com.accounting.client.frontend.customer;

import java.util.List;

import com.accounting.client.remote.Services;
import com.accounting.client.remote.obj.Customer;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.CollectionUtil;
import corelib.client.Util;
import corelib.client.delegate.DataCallback;
import corelib.client.search.JSONSearchRequest;
import corelib.client.search.JSONSearchResult;
import corelib.client.search.Operation;
import corelib.client.search.SearchCriteria;
import corelib.client.ui.SimpleMask;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.button.Button;
import corelib.client.ui.widget.form.LazyInitializer;
import corelib.client.ui.widget.form.SimpleLabel;
import corelib.client.ui.widget.form.TextField;
import corelib.client.ui.widget.layout.RelativeColumn;
import corelib.client.ui.widget.panel.SimpleSearchPanel;

public class CustomerSearchPanel extends SimpleSearchPanel {
	
	private TextField nameField;
	private TextField emailField;
	private TextField addressField;
	private TextField phoneNumberField;
	
	private CustomerManager manager;
	private DataCallback<JSONSearchResult<Customer>> searchAction;
	public final static int MaxRecordsPerPage = 10;
	
	public CustomerSearchPanel(CustomerManager manager, DataCallback<JSONSearchResult<Customer>> dataCallback){
		this.manager = manager;
		this.searchAction = dataCallback;
		this.setWidth(350);
	}
	
	@Override
	protected Widget getMaskWidget() {
		return manager.getCustomerTable();
	}

	@Override
	protected Widget createContentPanel() {
		int lw  = 100;
		int fw  = 250;
		int gap = 8;
		
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		
		RelativeColumn col = new RelativeColumn();
		col.setRowSpacing(12);
		col.addRow()
		   .add(new SimpleLabel("姓名").setBold(true),lw)
		   .addSpace(gap)
		   .add(nameField = LazyInitializer.createTextBox("姓名"), fw);
		
		col.addRow()
		   .add(new SimpleLabel("电子邮件").setBold(true),lw)
		   .addSpace(gap)
		   .add(emailField = LazyInitializer.createTextBox("电子邮件"), fw);
		
		col.addRow()
		   .add(new SimpleLabel("电话号码").setBold(true),lw)
		   .addSpace(gap)
		   .add(phoneNumberField = LazyInitializer.createTextBox("电话号码"), fw);
		
		col.addRow()
		   .add(new SimpleLabel("地址").setBold(true),lw)
		   .addSpace(gap)
		   .add(addressField = LazyInitializer.createTextBox("地址"), fw);
		
		Button searchBtn = new Button("搜索", new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				search();
			}
		});
		
		Button resetBtn = new Button("重置",new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				reset();
			}
		});
		
		col.addRow().add(searchBtn,70).addSpace(gap).add(resetBtn,70);
		panel.add(col.getPanel());
		return panel;
	}

	@Override
	protected JSONSearchRequest createSearchRequest() {
		
		String name 	   = nameField.getTrimmedText();
		String email 	   = emailField.getTrimmedText();
		String phoneNumber = phoneNumberField.getTrimmedText();
		String address     = addressField.getTrimmedText();
		
		JSONSearchRequest request = new JSONSearchRequest();
		request.setMatchAll(true);
		request.setOffset(0);
		request.setLimit(MaxRecordsPerPage);
		
		List<SearchCriteria> criterias = CollectionUtil.newArrayList();
		request.setCriterias(criterias);
		if (!Util.isBlank(name)) {
			 criterias.add(new SearchCriteria("Name",Operation.Contains.getValue(), name));
		}
		if (!Util.isBlank(email)){
			 criterias.add(new SearchCriteria("Email",Operation.Contains.getValue(), email));
		}
		if (!Util.isBlank(phoneNumber)){
			 criterias.add(new SearchCriteria("PhoneNumber",Operation.Contains.getValue(), phoneNumber));
		}
		if (!Util.isBlank(address)){
			 criterias.add(new SearchCriteria("Address",Operation.Contains.getValue(), address));
		}
		return request;
	}
	
	private void reset(){
		this.nameField.setText(Util.EMPTY_STRING,false);
		this.emailField.setText(Util.EMPTY_STRING,false);
		this.phoneNumberField.setText(Util.EMPTY_STRING,false);
		this.addressField.setText(Util.EMPTY_STRING,false);
		search();
	}
	
	private void search() {
		SimpleMask.show(getMaskWidget(), "搜索...");
		String passport = SessionManager.getPassport();
		Services.CustomerService.SearchCustomers(passport, createSearchRequest(),
			new AsyncCallback<JSONSearchResult<Customer>>(){
				@Override
				public void onFailure(Throwable caught) {
					SimpleMask.hide(getMaskWidget());
					ErrorHandler.handleServiceError(caught);
				}
				
				@Override
				public void onSuccess(JSONSearchResult<Customer> result) {
					SimpleMask.hide(getMaskWidget());
					if (searchAction != null){
						searchAction.onCompleted(result);
					}
				}
			}
		);
	}

}
